package com.banciyuan.bcywebview.biz.ask.answer;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.activity.BaseActivity;
import com.banciyuan.bcywebview.base.helper.ActionbarHelper;
import com.banciyuan.bcywebview.base.helper.FlagUtils;
import com.banciyuan.bcywebview.base.helper.ActionbarHelper.OnActionCallbacks;
import com.banciyuan.bcywebview.base.helper.data.UserDataHelper;
import com.banciyuan.bcywebview.base.myinterface.PushActionInterface;
import com.banciyuan.bcywebview.base.view.toast.MyToast;
import com.banciyuan.bcywebview.utils.http.NetUtils;
import com.banciyuan.bcywebview.utils.intent.gotoUtil;
import com.banciyuan.bcywebview.utils.string.StringUtil;

/**
 * @author hfxt2008
 * 
 * 首页个人页面->ASK页面->问题页面->回答页面
 */
public class WriteAnswerActivity extends BaseActivity implements OnActionCallbacks {

	private PushActionInterface mInterface;

	private ActionbarHelper mActionbarHelper;
	private View mActionBarView;

	private EditText mContentEt;
	private TextView mQuestionTv;

	private WriteAnswerHelper mHelper;
	private TextView tv_save;
	private ProgressDialog mDialog;
	private Boolean CLICK_FLAG=false;
	private String ua_id = "";
	private String question = "";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.write_answer_layout);
		initArgs();
		initActionbar();
		initUi();
		initAction();
	}

	@Override
	protected void initArgs() {
		if (!UserDataHelper.ifLogin(this) || TextUtils.isEmpty(UserDataHelper.getInstance(this).getToken())) {
			MyToast.show(this, getString(R.string.loginfirst));
			finish();
		}
		mInterface = new PushActionInterface() {
			@Override
			public void onNetError(String result) {
				onNetworkError(result);
			}
			@Override
			public void PushSuccess(String result) {
				pushSuccess(result);
			}
		};
		ua_id = StringUtil.getString(getIntent(), gotoUtil.INTENT_VALUE_ONE);
		question = StringUtil.getString(getIntent(), gotoUtil.INTENT_VALUE_TWO);
		mHelper = new WriteAnswerHelper(this, mInterface, ua_id);
	}

	@Override
	protected void initActionbar() {
		mActionBarView = findViewById(R.id.base_action_bar);
		mActionbarHelper = new ActionbarHelper(this, mActionBarView);
		mActionbarHelper.addItemOneText(getString(R.string.respones));
		mActionbarHelper.setTitle("");
		mActionbarHelper.setActionCallbacks(this);
		tv_save= (TextView) findViewById(R.id.base_action_bar_home_text_item);
		tv_save.setTextColor(getResources().getColorStateList(R.color.font_color));
	}

	@Override
	protected void initUi() {
		mContentEt = (EditText) findViewById(R.id.write_question_content_et);
		mContentEt.requestFocus();
		mQuestionTv = (TextView) findViewById(R.id.write_question_question_tv);
		mQuestionTv.setText(question);
		mDialog = new ProgressDialog(this);
		mDialog.setCancelable(false);
		mDialog.setMessage(this.getResources().getString(R.string.all_send_message));
	}

	@Override
	protected void initAction() {
		mContentEt.addTextChangedListener(new TextWatcher() {
			@Override
			public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

			}

			@Override
			public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

			}

			@Override
			public void afterTextChanged(Editable editable) {
				if (editable.length() == 0) {
					tv_save.setTextColor(getResources().getColorStateList(R.color.font_color));
					CLICK_FLAG = false;
					return;
				}
				tv_save.setTextColor(getResources().getColorStateList(R.color.pink));
				CLICK_FLAG = true;
			}
		});
	}

	@Override
	public void onBackClick() {
		finish();
	}

	@Override
	public void onActionClick(int id) {
		switch (id) {
		case R.id.base_action_bar_home_text_item:
			if (CLICK_FLAG) {
				if (TextUtils.isEmpty(mContentEt.getText().toString())) {
                    MyToast.show(this, getString(R.string.noemptycotent));
                    return;
                }
				if(!NetUtils.checkNetwork(this)) {
                    MyToast.show(this, getString(R.string.net_check_first));
                    return ;
                }
				mDialog.show();
				mHelper.setContent(mContentEt.getText().toString());
				mHelper.doPush();
			}
			break;
		default:
			break;
		}
	}

	public void onNetworkError(String result) {
		mDialog.dismiss();
		MyToast.show(this, result);
	}

	public void pushSuccess(String result) {
		setResult(RESULT_OK);
		mDialog.dismiss();
		finish();
	}
}